home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / pcl-rev4.lha / cache.lisp < prev    next >
Lisp/Scheme  |  1990-10-10  |  39KB  |  1,081 lines

  1. ;;;-*-Mode:LISP; Package:(PCL LISP 1000); Base:10; Syntax:Common-lisp -*-
  2. ;;;
  3. ;;; *************************************************************************
  4. ;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
  5. ;;; All rights reserved.
  6. ;;;
  7. ;;; Use and copying of this software and preparation of derivative works
  8. ;;; based upon this software are permitted.  Any distribution of this
  9. ;;; software or derivative works must comply with all applicable United
  10. ;;; States export control laws.
  11. ;;; 
  12. ;;; This software is made available AS IS, and Xerox Corporation makes no
  13. ;;; warranty about the software, its performance or its conformity to any
  14. ;;; specification.
  15. ;;; 
  16. ;;; Any person obtaining a copy of this software is requested to send their
  17. ;;; name and post office or electronic mail address to:
  18. ;;;   CommonLoops Coordinator
  19. ;;;   Xerox PARC
  20. ;;;   3333 Coyote Hill Rd.
  21. ;;;   Palo Alto, CA 94304
  22. ;;; (or send Arpanet mail to CommonLoops-Coordinator.pa@Xerox.arpa)
  23. ;;;
  24. ;;; Suggestions, comments and requests for improvements are also welcome.
  25. ;;; *************************************************************************
  26. ;;;
  27. ;;; The basics of the PCL wrapper cache mechanism.
  28. ;;;
  29.  
  30. (in-package 'pcl)
  31. ;;;
  32. ;;; The caching algorithm implemented:
  33. ;;;
  34. ;;; << put a paper here >>
  35. ;;;
  36. ;;; For now, understand that as far as most of this code goes, a cache has
  37. ;;; two important properties.  The first is the number of wrappers used as
  38. ;;; keys in each cache line.  Throughout this code, this value is always
  39. ;;; called NKEYS.  The second is whether or not the cache lines of a cache
  40. ;;; store a value.  Throughout this code, this always called VALUEP.
  41. ;;;
  42. ;;; Depending on these values, there are three kinds of caches.
  43. ;;;
  44. ;;; NKEYS = 1, VALUEP = NIL
  45. ;;;
  46. ;;; In this kind of cache, each line is 1 word long.  No cache locking is
  47. ;;; needed since all read's in the cache are a single value.  Nevertheless
  48. ;;; line 0 (location 0) is reserved, to ensure that invalid wrappers will
  49. ;;; not get a first probe hit.
  50. ;;;
  51. ;;; To keep the code simpler, a cache lock count does appear in location 0
  52. ;;; of these caches, that count is incremented whenever data is written to
  53. ;;; the cache.  But, the actual lookup code (see make-dlap) doesn't need to
  54. ;;; do locking when reading the cache.
  55. ;;; 
  56. ;;;
  57. ;;; NKEYS = 1, VALUEP = T
  58. ;;;
  59. ;;; In this kind of cache, each line is 2 words long.  Cache locking must
  60. ;;; be done to ensure the synchronization of cache reads.  Line 0 of the
  61. ;;; cache (location 0) is reserved for the cache lock count.  Location 1
  62. ;;; of the cache is unused (in effect wasted).
  63. ;;; 
  64. ;;; NKEYS > 1
  65. ;;;
  66. ;;; In this kind of cache, the 0 word of the cache holds the lock count.
  67. ;;; The 1 word of the cache is line 0.  Line 0 of these caches is not
  68. ;;; reserved.
  69. ;;;
  70. ;;; This is done because in this sort of cache, the overhead of doing the
  71. ;;; cache probe is high enough that the 1+ required to offset the location
  72. ;;; is not a significant cost.  In addition, because of the larger line
  73. ;;; sizes, the space that would be wasted by reserving line 0 to hold the
  74. ;;; lock count is more significant.
  75. ;;;
  76.  
  77.  
  78. ;;;
  79. ;;; Caches
  80. ;;;
  81. ;;; A cache is essentially just a vector.  The use of the individual `words'
  82. ;;; in the vector depends on particular properties of the cache as described
  83. ;;; above.
  84. ;;;
  85. ;;; This defines an abstraction for caches in terms of their most obvious
  86. ;;; implementation as simple vectors.  But, please notice that part of the
  87. ;;; implementation of this abstraction, is the function lap-out-cache-ref.
  88. ;;; This means that most port-specific modifications to the implementation
  89. ;;; of caches will require corresponding port-specific modifications to the
  90. ;;; lap code assembler.
  91. ;;;
  92. (defmacro cache-ref (cache location)
  93.   `(svref (the simple-vector ,cache) (the fixnum ,location)))
  94.  
  95. (defun emit-cache-ref (cache-operand location-operand)
  96.   (operand :iref cache-operand location-operand))
  97.  
  98.  
  99. (defun cache-size (cache)
  100.   (array-dimension (the simple-vector cache) 0))
  101.  
  102. (defun allocate-cache (size)
  103.   (make-array size :adjustable nil))
  104.  
  105. (defmacro cache-lock-count (cache)
  106.   `(cache-ref ,cache 0))
  107.  
  108. (defun flush-cache-internal (cache)
  109.   (without-interrupts  
  110.     (fill (the simple-vector cache) nil)
  111.     (setf (cache-lock-count cache) 0))
  112.   cache)
  113.  
  114. (defmacro modify-cache (cache &body body)
  115.   `(without-interrupts
  116.      (multiple-value-prog1
  117.        (progn ,@body)
  118.        (let ((old-count (cache-lock-count ,cache)))
  119.      (setf (cache-lock-count ,cache)
  120.            (if (= old-count most-positive-fixnum) 1 (1+ old-count)))))))
  121.  
  122.  
  123.  
  124. ;;;
  125. ;;; Some facilities for allocation and freeing caches as they are needed.
  126. ;;; This is done on the assumption that a better port of PCL will arrange
  127. ;;; to cons these all the same static area.  Given that, the fact that
  128. ;;; PCL tries to reuse them should be a win.
  129. ;;; 
  130. (defvar *free-caches* (make-hash-table :size 16))
  131.  
  132. ;;;
  133. ;;; Return a cache that has had flush-cache-internal called on it.  This
  134. ;;; returns a cache of exactly the size requested, it won't ever return a
  135. ;;; larger cache.
  136. ;;; 
  137. (defun get-cache (size)
  138.   (let ((entry (gethash size *free-caches*)))
  139.     (without-interrupts
  140.       (cond ((null entry)
  141.          (setf (gethash size *free-caches*) (cons 0 nil))
  142.          (get-cache size))
  143.         ((null (cdr entry))
  144.          (incf (car entry))
  145.          (flush-cache-internal (allocate-cache size)))
  146.         (t
  147.          (let ((cache (cdr entry)))
  148.            (setf (cdr entry) (cache-ref cache 0))
  149.            (flush-cache-internal cache)))))))
  150.  
  151. (defun free-cache (cache)
  152.   (let ((entry (gethash (cache-size cache) *free-caches*)))
  153.     (without-interrupts
  154.       (if (null entry)
  155.       (error "Attempt to free a cache not allocated by GET-CACHE.")
  156.       (let ((thread (cdr entry)))
  157.         (loop (unless thread (return))
  158.           (when (eq thread cache) (error "Freeing a cache twice."))
  159.           (setq thread (cache-ref thread 0)))      
  160.         (flush-cache-internal cache)        ;Help the GC
  161.         (setf (cache-ref cache 0) (cdr entry))
  162.         (setf (cdr entry) cache)
  163.         nil)))))
  164.  
  165. ;;;
  166. ;;; This is just for debugging and analysis.  It shows the state of the free
  167. ;;; cache resource.
  168. ;;; 
  169. (defun show-free-caches ()
  170.   (let ((elements ()))
  171.     (maphash #'(lambda (s e) (push (list s e) elements)) *free-caches*)
  172.     (setq elements (sort elements #'< :key #'car))
  173.     (dolist (e elements)
  174.       (let* ((size (car e))
  175.          (entry (cadr e))
  176.          (allocated (car entry))
  177.          (head (cdr entry))
  178.          (free 0))
  179.     (loop (when (null head) (return t))
  180.           (setq head (cache-ref head 0))
  181.           (incf free))
  182.     (format t
  183.         "~&There  ~4D are caches of size ~4D. (~D free  ~3D%)"
  184.         allocated
  185.         size
  186.         free
  187.         (floor (* 100 (/ free (float allocated)))))))))
  188.  
  189.  
  190. ;;;
  191. ;;; Wrapper cache numbers
  192. ;;; 
  193.  
  194. ;;;
  195. ;;; The constant WRAPPER-CACHE-NUMBER-ADDS-OK controls the number of non-zero
  196. ;;; bits wrapper cache numbers will have.
  197. ;;;
  198. ;;; The value of this constant is the number of wrapper cache numbers which
  199. ;;; can be added and still be certain the result will be a fixnum.  This is
  200. ;;; used by all the code that computes primary cache locations from multiple
  201. ;;; wrappers.
  202. ;;;
  203. ;;; The value of this constant is used to derive the next two which are the
  204. ;;; forms of this constant which it is more convenient for the runtime code
  205. ;;; to use.
  206. ;;; 
  207. (eval-when (compile load eval)
  208.  
  209. (defconstant wrapper-cache-number-adds-ok 4)
  210.  
  211. (defconstant wrapper-cache-number-length
  212.          (- (integer-length most-positive-fixnum)
  213.         wrapper-cache-number-adds-ok))
  214.  
  215. (defconstant wrapper-cache-number-mask
  216.          (1- (expt 2 wrapper-cache-number-length)))
  217.  
  218.  
  219. (defvar *get-wrapper-cache-number* (make-random-state))
  220.  
  221. (defun get-wrapper-cache-number ()
  222.   (let ((n 0))
  223.     (loop
  224.       (setq n
  225.         (logand wrapper-cache-number-mask
  226.             (random most-positive-fixnum *get-wrapper-cache-number*)))
  227.       (unless (zerop n) (return n)))))
  228.  
  229.  
  230. (unless (> wrapper-cache-number-length 8)
  231.   (error "In this implementation of Common Lisp, fixnums are so small that~@
  232.           wrapper cache numbers end up being only ~D bits long.  This does~@
  233.           not actually keep PCL from running, but it may degrade cache~@
  234.           performance.~@
  235.           You may want to consider changing the value of the constant~@
  236.           WRAPPER-CACHE-NUMBER-ADDS-OK.")))
  237.  
  238.  
  239. ;;;
  240. ;;; wrappers themselves
  241. ;;;
  242. ;;; This caching algorithm requires that wrappers have more than one wrapper
  243. ;;; cache number.  You should think of these multiple numbers as being in
  244. ;;; columns.  That is, for a given cache, the same column of wrapper cache
  245. ;;; numbers will be used.
  246. ;;;
  247. ;;; If at some point the cache distribution of a cache gets bad, the cache
  248. ;;; can be rehashed by switching to a different column.
  249. ;;;
  250. ;;; The columns are referred to by field number which is that number which,
  251. ;;; when used as a second argument to wrapper-ref, will return that column
  252. ;;; of wrapper cache number.
  253. ;;;
  254. ;;; This code is written to allow flexibility as to how many wrapper cache
  255. ;;; numbers will be in each wrapper, and where they will be located.  It is
  256. ;;; also set up to allow port specific modifications to `pack' the wrapper
  257. ;;; cache numbers on machines where the addressing modes make that a good
  258. ;;; idea.
  259. ;;; 
  260. (eval-when (compile load eval)
  261. (defconstant wrapper-layout
  262.          '(number
  263.            number
  264.            number
  265.            number
  266.            number
  267.            number
  268.            number
  269.            number
  270.            state
  271.            instance-slots-layout
  272.            class-slots
  273.            class))
  274. )
  275.  
  276. (eval-when (compile load eval)
  277.  
  278. (defun wrapper-field (type)
  279.   (position type wrapper-layout))
  280.  
  281. (defun next-wrapper-field (field-number)
  282.   (position (nth field-number wrapper-layout)
  283.         wrapper-layout
  284.         :start (1+ field-number)))
  285.  
  286. );eval-when
  287.  
  288. (defmacro wrapper-ref (wrapper n)
  289.   `(svref ,wrapper ,n))
  290.  
  291. (defun emit-wrapper-ref (wrapper-operand field-operand)
  292.   (operand :iref wrapper-operand field-operand))
  293.  
  294.           
  295. (defmacro wrapper-state (wrapper)
  296.   `(wrapper-ref ,wrapper ,(wrapper-field 'state)))
  297.  
  298. (defmacro wrapper-instance-slots-layout (wrapper)
  299.   `(wrapper-ref ,wrapper ,(wrapper-field 'instance-slots-layout)))
  300.  
  301. (defmacro wrapper-class-slots (wrapper)
  302.   `(wrapper-ref ,wrapper ,(wrapper-field 'class-slots)))
  303.  
  304. (defmacro wrapper-class (wrapper)
  305.   `(wrapper-ref ,wrapper ,(wrapper-field 'class)))
  306.  
  307.  
  308. (defmacro make-wrapper-internal ()
  309.   `(let ((wrapper (make-array ,(length wrapper-layout) :adjustable nil)))
  310.      ,@(gathering1 (collecting)
  311.      (iterate ((i (interval :from 0))
  312.            (desc (list-elements wrapper-layout)))
  313.        (ecase desc
  314.          (number
  315.           (gather1 `(setf (wrapper-ref wrapper ,i)
  316.                   (get-wrapper-cache-number))))
  317.          ((state instance-slots-layout class-slots class)))))
  318.      (setf (wrapper-state wrapper) 't)     
  319.      wrapper))
  320.  
  321. (defun make-wrapper (class)
  322.   (let ((wrapper (make-wrapper-internal)))
  323.     (setf (wrapper-class wrapper) class)
  324.     wrapper))
  325.  
  326. ;;;
  327. ;;; The wrapper cache machinery provides general mechanism for trapping on
  328. ;;; the next access to any instance of a given class.  This mechanism is
  329. ;;; used to implement the updating of instances when the class is redefined
  330. ;;; (make-instances-obsolete).  The same mechanism is also used to update
  331. ;;; generic function caches when there is a change to the supers of a class.
  332. ;;;
  333. ;;; Basically, a given wrapper can be valid or invalid.  If it is invalid,
  334. ;;; it means that any attempt to do a wrapper cache lookup using the wrapper
  335. ;;; should trap.  Also, methods on slot-value-using-class check the wrapper
  336. ;;; validity as well.  This is done by calling check-wrapper-validity.
  337. ;;; 
  338.  
  339. (defun invalid-wrapper-p (wrapper)
  340.   (neq (wrapper-state wrapper) 't))
  341.  
  342. (defvar *previous-nwrappers* (make-hash-table))
  343.  
  344. (defun invalidate-wrapper (owrapper state nwrapper)
  345.   (ecase state
  346.     ((flush obsolete)
  347.      (let ((new-previous ()))
  348.        ;;
  349.        ;; First off, a previous call to invalidate-wrapper may have recorded
  350.        ;; owrapper as an nwrapper to update to.  Since owrapper is about to
  351.        ;; be invalid, it no longer makes sense to update to it.
  352.        ;;
  353.        ;; We go back and change the previously invalidated wrappers so that
  354.        ;; they will now update directly to nwrapper.  This corresponds to a
  355.        ;; kind of transitivity of wrapper updates.
  356.        ;; 
  357.        (dolist (previous (gethash owrapper *previous-nwrappers*))
  358.      (when (eq state 'obsolete)
  359.        (setf (car previous) 'obsolete))
  360.      (setf (cadr previous) nwrapper)
  361.      (push previous new-previous))
  362.        
  363.        (iterate ((type (list-elements wrapper-layout))
  364.          (i (interval :from 0)))
  365.      (when (eq type 'number) (setf (wrapper-ref owrapper i) 0)))
  366.        (push (setf (wrapper-state owrapper) (list state nwrapper))
  367.          new-previous)
  368.        
  369.        (setf (gethash owrapper *previous-nwrappers*) ()
  370.          (gethash nwrapper *previous-nwrappers*) new-previous)))))
  371.  
  372. (defun check-wrapper-validity (instance)
  373.   (let* ((owrapper (wrapper-of instance))
  374.      (state (wrapper-state owrapper)))
  375.     (if (eq state  't)
  376.     owrapper
  377.     (let ((nwrapper
  378.         (ecase (car state)
  379.           (flush
  380.             (flush-cache-trap owrapper (cadr state) instance))
  381.           (obsolete
  382.             (obsolete-instance-trap owrapper (cadr state) instance)))))
  383.       ;;
  384.       ;; This little bit of error checking is superfluous.  It only
  385.       ;; checks to see whether the person who implemented the trap
  386.       ;; handling screwed up.  Since that person is hacking internal
  387.       ;; PCL code, and is not a user, this should be needless.  Also,
  388.       ;; since this directly slows down instance update and generic
  389.       ;; function cache refilling, feel free to take it out sometime
  390.       ;; soon.
  391.       ;; 
  392.       (cond ((neq nwrapper (wrapper-of instance))
  393.          (error "Wrapper returned from trap not wrapper of instance."))
  394.         ((invalid-wrapper-p nwrapper)
  395.          (error "Wrapper returned from trap invalid.")))
  396.       nwrapper))))
  397.  
  398.  
  399.  
  400. (defun compute-line-size (nelements) (expt 2 (ceiling (log nelements 2))))
  401.  
  402. (defun compute-cache-parameters (nkeys valuep nlines-or-cache)
  403.   (declare (values cache-mask actual-size line-size nlines))
  404.   (flet ((compute-mask (cache-size line-size)
  405.        (logxor (1- cache-size) (1- line-size))))
  406.     (if (= nkeys 1)
  407.     (let* ((line-size (if valuep 2 1))
  408.            (cache-size (if (numberp nlines-or-cache)
  409.                    (* line-size
  410.                   (expt 2 (ceiling (log nlines-or-cache 2))))
  411.                    (cache-size nlines-or-cache))))
  412.       (values (compute-mask cache-size line-size)
  413.           cache-size
  414.           line-size
  415.           (/ cache-size line-size)))
  416.     (let* ((line-size (compute-line-size (+ nkeys (if valuep 1 0))))
  417.            (cache-size (if (numberp nlines-or-cache)
  418.                    (* line-size 
  419.                   (expt 2 (ceiling (log nlines-or-cache 2))))
  420.                    (1- (cache-size nlines-or-cache)))))
  421.       (values (compute-mask cache-size line-size)
  422.           (1+ cache-size)
  423.           line-size
  424.           (/ cache-size line-size))))))
  425.  
  426.  
  427.  
  428. ;;;
  429. ;;; The various implementations of computing a primary cache location from
  430. ;;; wrappers.  Because some implementations of this must run fast there are
  431. ;;; several implementations of the same algorithm.
  432. ;;;
  433. ;;; The algorithm is:
  434. ;;;
  435. ;;;  SUM       over the wrapper cache numbers,
  436. ;;;  ENSURING  that the result is a fixnum
  437. ;;;  MASK      the result against the mask argument.
  438. ;;;
  439. ;;;
  440.  
  441. ;;;
  442. ;;; COMPUTE-PRIMARY-CACHE-LOCATION
  443. ;;; 
  444. ;;; The basic functional version.  This is used by the cache miss code to
  445. ;;; compute the primary location of an entry.  
  446. ;;;
  447. (defun compute-primary-cache-location (field mask wrappers)
  448.   (if (not (consp wrappers))
  449.       (logand mask (wrapper-ref wrappers field))
  450.       (let ((location 0))
  451.     (iterate ((wrapper (list-elements wrappers))
  452.           (i (interval :from 0)))
  453.       ;;
  454.       ;; First add the cache number of this wrapper to location.
  455.       ;; 
  456.       (let ((wrapper-cache-number (wrapper-ref wrapper field)))
  457.         (if (zerop wrapper-cache-number)
  458.         (return-from compute-primary-cache-location 0)
  459.         (setq location (+ location wrapper-cache-number))))
  460.       ;;
  461.       ;; Then, if we are working with lots of wrappers, deal with
  462.       ;; the wrapper-cache-number-mask stuff.
  463.       ;; 
  464.       (when (and (not (zerop i))
  465.              (zerop (mod i wrapper-cache-number-adds-ok)))
  466.         (setq location
  467.           (logand location wrapper-cache-number-mask))))
  468.     (1+ (logand mask location)))))
  469.  
  470. ;;;
  471. ;;; COMPUTE-PRIMARY-CACHE-LOCATION-FROM-LOCATION
  472. ;;;
  473. ;;; This version is called on a cache line.  It fetches the wrappers from
  474. ;;; the cache line and determines the primary location.  Various parts of
  475. ;;; the cache filling code call this to determine whether it is appropriate
  476. ;;; to displace a given cache entry.
  477. ;;; 
  478. ;;; If this comes across a wrapper whose cache-no is 0, it returns the symbol
  479. ;;; invalid to suggest to its caller that it would be provident to blow away
  480. ;;; the cache line in question.
  481. ;;;
  482. (defun compute-primary-cache-location-from-location (field cache location mask nkeys)
  483.   (let ((result 0))
  484.     (dotimes (i nkeys)
  485.       (let* ((wrapper (cache-ref cache (+ i location)))
  486.          (wcn (wrapper-ref wrapper field)))
  487.     (setq result (+ result wcn)))
  488.       (when (and (not (zerop i))
  489.          (zerop (mod i wrapper-cache-number-adds-ok)))
  490.     (setq result (logand result wrapper-cache-number-mask)))
  491.       )    
  492.     (if (= nkeys 1)
  493.     (logand mask result)
  494.     (1+ (logand mask result)))))
  495.  
  496. (defun emit-1-wrapper-compute-primary-cache-location (wrapper primary wrapper-cache-no)
  497.   (with-lap-registers ((mask index))
  498.     (let ((field wrapper-cache-no))
  499.       (flatten-lap
  500.         (opcode :move (operand :cvar 'mask) mask)
  501.         (opcode :move (operand :cvar 'field) field)
  502.         (opcode :move (emit-wrapper-ref wrapper field) wrapper-cache-no)
  503.         (opcode :move (operand :ilogand wrapper-cache-no mask) primary)))))
  504.  
  505. (defun emit-n-wrapper-compute-primary-cache-location (wrappers primary miss-label)
  506.   (with-lap-registers ((field index)
  507.                (mask index))
  508.     (let ((add-wrapper-cache-numbers
  509.        (flatten-lap
  510.         (gathering1 (flattening-lap)
  511.            (iterate ((wrapper (list-elements wrappers))
  512.              (i (interval :from 1)))
  513.          (gather1
  514.           (with-lap-registers ((wrapper-cache-no index))
  515.             (flatten-lap
  516.              (opcode :move (emit-wrapper-ref wrapper field) wrapper-cache-no)
  517.              (opcode :izerop wrapper-cache-no miss-label)
  518.              (opcode :move (operand :i+ primary wrapper-cache-no) primary)
  519.              (when (zerop (mod i wrapper-cache-number-adds-ok))
  520.                (opcode :move (operand :ilogand primary mask) primary))))))))))
  521.       (flatten-lap
  522.        (opcode :move (operand :constant 0) primary)
  523.        (opcode :move (operand :cvar 'field) field)
  524.        (opcode :move (operand :cvar 'mask) mask)
  525.        add-wrapper-cache-numbers
  526.        (opcode :move (operand :ilogand primary mask) primary)
  527.        (opcode :move (operand :i1+ primary) primary)))))
  528.  
  529.  
  530.  
  531. ;;;
  532. ;;;  NIL              means nothing so far, no actual arg info has NILs
  533. ;;;                   in the metatype
  534. ;;;  CLASS            seen all sorts of metaclasses
  535. ;;;                   (specifically, more than one of the next 4 values)
  536. ;;;  T                means everything so far is the class T
  537. ;;;  STANDARD-CLASS   seen only standard classes
  538. ;;;  BUILT-IN-CLASS   seen only built in classes
  539. ;;;  STRUCTURE-CLASS  seen only structure classes
  540. ;;;  
  541. (defun raise-metatype (metatype new-specializer)
  542.   (let ((standard  (find-class 'standard-class))
  543.     (fsc       (find-class 'funcallable-standard-class))
  544. ;    (structure (find-class 'structure-class))
  545.     (built-in  (find-class 'built-in-class)))
  546.     (flet ((specializer->metatype (x)
  547.          (let ((meta-specializer 
  548.              (if (and (eq *boot-state* 'complete)
  549.                   (eql-specializer-p x))
  550.              (class-of (class-of (eql-specializer-object x)))
  551.              (class-of x))))
  552.            (cond ((eq x *the-class-t*) t)
  553.              ((*subtypep meta-specializer standard)  'standard-instance)
  554.              ((*subtypep meta-specializer fsc)       'standard-instance)
  555. ;                    ((*subtypep meta-specializer structure) 'structure-instance)
  556.              ((*subtypep meta-specializer built-in)  'built-in-instance)
  557.              (t (error "PCL can not handle the specializer ~S (meta-specializer ~S)."
  558.                    new-specializer meta-specializer))))))
  559.       ;;
  560.       ;; We implement the following table.  The notation is
  561.       ;; that X and Y are distinct meta specializer names.
  562.       ;; 
  563.       ;;   NIL    <anything>    ===>  <anything>
  564.       ;;    X      X            ===>      X
  565.       ;;    X      Y            ===>    CLASS
  566.       ;;    
  567.       (let ((new-metatype (specializer->metatype new-specializer)))
  568.     (cond ((null metatype) new-metatype)
  569.           ((eq metatype new-metatype) new-metatype)
  570.           (t 'class))))))
  571.  
  572.  
  573. (defun emit-fetch-wrapper (metatype argument dest miss-label &optional slot)
  574.   (let ((exit-emit-fetch-wrapper (make-symbol "exit-emit-fetch-wrapper")))
  575.     (with-lap-registers ((arg t))
  576.       (ecase metatype
  577.     (standard-instance
  578.       (let ((get-std-inst-wrapper (make-symbol "get-std-inst-wrapper"))
  579.         (get-fsc-inst-wrapper (make-symbol "get-fsc-inst-wrapper")))
  580.         (flatten-lap
  581.           (opcode :move (operand :arg argument) arg)
  582.           (opcode :std-instance-p arg get-std-inst-wrapper)       ;is it a std wrapper?
  583.           (opcode :fsc-instance-p arg get-fsc-inst-wrapper)       ;is it a fsc wrapper?
  584.           (opcode :go miss-label)
  585.           (opcode :label get-fsc-inst-wrapper)
  586.           (opcode :move (operand :fsc-wrapper arg) dest)       ;get fsc wrapper
  587.           (and slot
  588.            (opcode :move (operand :fsc-slots arg) slot))
  589.           (opcode :go exit-emit-fetch-wrapper)
  590.           (opcode :label get-std-inst-wrapper)
  591.           (opcode :move (operand :std-wrapper arg) dest)       ;get std wrapper
  592.           (and slot
  593.            (opcode :move (operand :std-slots arg) slot))
  594.           (opcode :label exit-emit-fetch-wrapper))))
  595.  
  596.     (class
  597.       (when slot (error "Can't do a slot reg for this metatype."))
  598.       (let ((get-std-inst-wrapper (make-symbol "get-std-inst-wrapper"))
  599.         (get-fsc-inst-wrapper (make-symbol "get-fsc-inst-wrapper"))
  600.         (get-built-in-wrapper (make-symbol "get-built-in-wrapper")))
  601.         (flatten-lap
  602.           (opcode :move (operand :arg argument) arg)
  603.           (opcode :std-instance-p arg get-std-inst-wrapper)
  604.           (opcode :fsc-instance-p arg get-fsc-inst-wrapper)
  605.           (opcode :built-in-instance-p arg get-built-in-wrapper)
  606.           ;; If the code falls through the checks above, there is a serious problem
  607.           (opcode :label get-fsc-inst-wrapper)
  608.           (opcode :move (operand :fsc-wrapper arg) dest)
  609.           (opcode :go exit-emit-fetch-wrapper)
  610.           (opcode :label get-built-in-wrapper)
  611.           (opcode :move (operand :built-in-wrapper arg) dest)
  612.           (opcode :go exit-emit-fetch-wrapper)
  613.           (opcode :label get-std-inst-wrapper)
  614.           (opcode :move (operand :std-wrapper arg) dest)
  615.           (opcode :label exit-emit-fetch-wrapper))))
  616.     (structure-instance 
  617.       (when slot (error "Can't do a slot reg for this metatype."))
  618.       (error "Not yet implemented"))
  619.     (built-in-instance
  620.       (when slot (error "Can't do a slot reg for this metatype."))
  621.       (let ((get-built-in-wrapper (make-symbol "get-built-in-wrapper")))
  622.         (flatten-lap
  623.           (opcode :move (operand :arg argument) arg)
  624.           (opcode :built-in-instance-p arg get-built-in-wrapper)
  625.           (opcode :go miss-label)
  626.           (opcode :label get-built-in-wrapper)
  627.           (opcode :move (operand :built-in-wrapper arg) dest))))))))
  628.  
  629.  
  630. ;;;
  631. ;;; Some support stuff for getting a hold of symbols that we need when
  632. ;;; building the discriminator codes.  Its ok for these to be interned
  633. ;;; symbols because we don't capture any user code in the scope in which
  634. ;;; these symbols are bound.
  635. ;;; 
  636.  
  637. (defvar *dfun-arg-symbols* '(.ARG0. .ARG1. .ARG2. .ARG3.))
  638.  
  639. (defun dfun-arg-symbol (arg-number)
  640.   (or (nth arg-number (the list *dfun-arg-symbols*))
  641.       (intern (format nil ".ARG~A." arg-number) *the-pcl-package*)))
  642.  
  643. (defvar *slot-vector-symbols* '(.SLOTS0. .SLOTS1. .SLOTS2. .SLOTS3.))
  644.  
  645. (defun slot-vector-symbol (arg-number)
  646.   (or (nth arg-number (the list *slot-vector-symbols*))
  647.       (intern (format nil ".SLOTS~A." arg-number) *the-pcl-package*)))
  648.  
  649. (defun make-dfun-lambda-list (metatypes applyp)
  650.   (gathering1 (collecting)
  651.     (iterate ((i (interval :from 0))
  652.           (s (list-elements metatypes)))
  653.       (progn s)
  654.       (gather1 (dfun-arg-symbol i)))
  655.     (when applyp
  656.       (gather1 '&rest)
  657.       (gather1 '.dfun-rest-arg.))))
  658.  
  659. (defun make-dlap-lambda-list (metatypes applyp)
  660.   (gathering1 (collecting)
  661.     (iterate ((i (interval :from 0))
  662.           (s (list-elements metatypes)))
  663.       (progn s)
  664.       (gather1 (dfun-arg-symbol i)))
  665.     (when applyp
  666.       (gather1 '&rest))))
  667.  
  668. (defun make-dfun-call (metatypes applyp fn-variable)
  669.   (let ((required
  670.       (gathering1 (collecting)
  671.         (iterate ((i (interval :from 0))
  672.               (s (list-elements metatypes)))
  673.           (progn s)
  674.           (gather1 (dfun-arg-symbol i))))))
  675.     (if applyp
  676.     `(apply   ,fn-variable ,@required .dfun-rest-arg.)
  677.     `(funcall ,fn-variable ,@required))))
  678.  
  679.  
  680. ;;;
  681. ;;; Here is where we actually fill, recache and expand caches.
  682. ;;;
  683. ;;; The function FILL-CACHE is the ONLY external entrypoint into this code.
  684. ;;; It returns 4 values:
  685. ;;;   a wrapper field number
  686. ;;;   a cache
  687. ;;;   a mask
  688. ;;;   an absolute cache size (the size of the actual vector)
  689. ;;;
  690. ;;;
  691. (defun fill-cache (field cache nkeys valuep limit-fn wrappers value)
  692.   (declare (values field cache mask size))
  693.   (fill-cache-internal field cache nkeys valuep limit-fn wrappers value))
  694.  
  695. (defun default-limit-fn (nlines)
  696.   (case nlines
  697.     ((1 2 4) 1)
  698.     ((8 16)  4)
  699.     (otherwise 6)))
  700.  
  701.  
  702. ;;;
  703. ;;; Its too bad Common Lisp compilers freak out when you have a defun with
  704. ;;; a lot of LABELS in it.  If I could do that I could make this code much
  705. ;;; easier to read and work with.
  706. ;;;
  707. ;;; Ahh Scheme...
  708. ;;; 
  709. ;;; In the absence of that, the following little macro makes the code that
  710. ;;; follows a little bit more reasonable.  I would like to add that having
  711. ;;; to practically write my own compiler in order to get just this simple
  712. ;;; thing is something of a drag.
  713. ;;;
  714. (eval-when (compile load eval)
  715.  
  716. (proclaim '(special *nkeys* *valuep* *limit-fn*))
  717.  
  718. (defvar *local-cache-functions*
  719.     `((cache     () .cache.)
  720.       (nkeys     () *nkeys*)
  721.       (valuep    () *valuep*)
  722.       (limit-fn  () *limit-fn*)
  723.       (line-size () .line-size.)
  724.       (mask      () .mask.)
  725.       (size      () .size.)
  726.       (nlines    () .nlines.)
  727.       ;;
  728.       ;; Return T IFF this cache location is reserved.  The only time
  729.       ;; this is true is for line number 0 of an nkeys=1 cache.  
  730.       ;;
  731.       (line-reserved-p (line)
  732.         (and (= (nkeys) 1)
  733.          (= line 0)))
  734.       ;;
  735.       ;; Given a line number, return the cache location.  This is the
  736.       ;; value that is the second argument to cache-ref.  Basically,
  737.       ;; this deals with the offset of nkeys>1 caches and multiplies
  738.       ;; by line size.  This returns nil if the line is reserved.
  739.       ;;       
  740.       (line-location (line)
  741.         (and (null (line-reserved-p line))
  742.          (if (= (nkeys) 1)
  743.              (* line (line-size))
  744.              (1+ (* line (line-size))))))
  745.       ;;
  746.       ;; Given a cache location, return the line.  This is the inverse
  747.       ;; of LINE-LOCATION.
  748.       ;;       
  749.       (location-line (location)
  750.         (if (= (nkeys) 1)
  751.         (/ location (line-size))
  752.         (/ (1- location) (line-size))))
  753.       ;;
  754.       ;; Given a line number, return the wrappers stored at that line.
  755.       ;; As usual, if nkeys=1, this returns a single value.  Only when
  756.       ;; nkeys>1 does it return a list.  An error is signalled if the
  757.       ;; line is reserved.
  758.       ;;
  759.       (line-wrappers (line)
  760.         (when (line-reserved-p line) (error "Line is reserved."))
  761.         (let ((location (line-location line)))
  762.           (if (= (nkeys) 1)
  763.           (cache-ref (cache) location)
  764.           (gathering1 (collecting)
  765.             (dotimes (i (nkeys))
  766.               (gather1 (cache-ref (cache) (+ location i))))))))
  767.       ;;
  768.       ;; Given a line number, return the value stored at that line.
  769.       ;; If valuep is NIL, this returns NIL.  As with line-wrappers,
  770.       ;; an error is signalled if the line is reserved.
  771.       ;; 
  772.       (line-value (line)
  773.         (when (line-reserved-p line) (error "Line is reserved."))
  774.         (and (valuep)
  775.          (cache-ref (cache) (+ (line-location line) (nkeys)))))
  776.       ;;
  777.       ;; Given a line number, return true IFF that line has data in
  778.       ;; it.  The state of the wrappers stored in the line is not
  779.       ;; checked.  An error is signalled if line is reserved.
  780.       (line-full-p (line)
  781.         (when (line-reserved-p line) (error "Line is reserved."))
  782.         (not (null (cache-ref (cache) (line-location line)))))
  783.       ;;
  784.       ;; Given a line number, return true IFF the line is full and
  785.       ;; there are no invalid wrappers in the line, and the line's
  786.       ;; wrappers are different from wrappers.
  787.       ;; An error is signalled if the line is reserved.
  788.       ;;
  789.       (line-valid-p (line wrappers)
  790.         (when (line-reserved-p line) (error "Line is reserved."))
  791.         (let ((loc (line-location line))
  792.           (wrappers-mismatch-p (null wrappers)))
  793.           (dotimes (i (nkeys) wrappers-mismatch-p)
  794.         (let ((wrapper (cache-ref (cache) (+ loc i))))
  795.           (when (or (null wrapper)
  796.                 (invalid-wrapper-p wrapper))
  797.             (return nil))
  798.           (unless (and wrappers
  799.                    (eq wrapper
  800.                    (if (consp wrappers) (pop wrappers) wrappers)))
  801.             (setq wrappers-mismatch-p t))))))
  802.       ;;
  803.       ;; How many unreserved lines separate line-1 and line-2.
  804.       ;;
  805.       (line-separation (line-1 line-2)
  806.         (let ((diff (- line-2 line-1)))
  807.           (cond ((zerop diff) diff)
  808.             ((plusp diff) diff)
  809.             (t
  810.              (if (line-reserved-p 0)
  811.              (1- (+ (- (nlines) line-1) line-2))
  812.              (+ (- (nlines) line-1) line-2))))))
  813.       ;;
  814.       ;; Given a cache line, get the next cache line.  This will not
  815.       ;; return a reserved line.
  816.       ;; 
  817.       (next-line (line)
  818.         (if (= line (1- (nlines)))
  819.         (if (line-reserved-p 0) 1 0)
  820.         (1+ line)))
  821.       ;;
  822.       ;; Given a line which has a valid entry in it, this will return
  823.       ;; the primary cache line of the wrappers in that line.  We just
  824.       ;; call COMPUTE-PRIMARY-CACHE-LOCATION-FROM-LOCATION, this is an
  825.       ;; easier packaging up of the call to it.
  826.       ;; 
  827.       (line-primary (field line)
  828.         (location-line
  829.           (compute-primary-cache-location-from-location
  830.         field (cache) (line-location line) (mask) (nkeys))))
  831.       ;;
  832.       ;;
  833.       (fill-line (line wrappers value)
  834.         (when (line-reserved-p line)
  835.           (error "Attempt to fill a reserved line."))
  836.         (let ((loc (line-location line)))
  837.           (cond ((= (nkeys) 1)
  838.              (setf (cache-ref (cache) loc) wrappers)
  839.              (when (valuep) (setf (cache-ref (cache) (1+ loc)) value)))
  840.             (t
  841.              (iterate ((i (interval :from 0))
  842.                    (w (list-elements wrappers)))
  843.                (setf (cache-ref (cache) (+ loc i)) w))
  844.              (when (valuep) (setf (cache-ref (cache) (+ loc (nkeys))) value))))))
  845.       ;;
  846.       ;; Blindly copy the contents of one cache line to another.  The
  847.       ;; contents of the <to> line are overwritten, so whatever was in
  848.       ;; there should already have been moved out.
  849.       ;;
  850.       ;; For convenience in debugging, this also clears out the from
  851.       ;; location after it has been copied.
  852.       ;;
  853.       (copy-line (from to)
  854.         (if (line-reserved-p to)
  855.         (error "Copying something into a reserved cache line.")
  856.         (let ((from-loc (line-location from))
  857.               (to-loc (line-location to)))
  858.           (modify-cache (cache)
  859.             (dotimes (i (line-size))
  860.               (setf (cache-ref (cache) (+ to-loc i))
  861.                 (cache-ref (cache) (+ from-loc i)))
  862.               (setf (cache-ref (cache) (+ from-loc i))
  863.                 nil))))))
  864.       ;;
  865.       ;;
  866.       ;;
  867.       (transfer-line (from-cache from-line to-cache to-line)
  868.         (if (line-reserved-p to-line)
  869.         (error "transfering something into a reserved cache line.")
  870.         (let ((from-loc (line-location from-line))
  871.               (to-loc (line-location to-line)))
  872.           (modify-cache to-cache
  873.             (dotimes (i (line-size))
  874.               (setf (cache-ref to-cache (+ to-loc i))
  875.                 (cache-ref from-cache (+ from-loc i))))))))
  876.       ))
  877.  
  878. (defmacro with-local-cache-functions ((cache) &body body &environment env)
  879.   `(let ((.cache. ,cache))
  880.      (declare (type simple-vector .cache.))
  881.      (multiple-value-bind (.mask. .size. .line-size. .nlines.)
  882.      (compute-cache-parameters *nkeys* *valuep* .cache.)
  883.        (declare (type fixnum .mask. .size. .line-size. .nlines.))
  884.        (progn .mask. .size. .line-size. .nlines.)
  885.        (labels ,(mapcar #'(lambda (fn) (assq fn *local-cache-functions*))
  886.             (pickup-local-cache-functions body env))
  887.      ,@body))))
  888.  
  889. (defun pickup-local-cache-functions (body env)
  890.   (let ((functions ())
  891.     (possible-functions (mapcar #'car *local-cache-functions*)))
  892.     (labels ((walk-function (form context env)
  893.            (declare (ignore env))
  894.            (when (and (eq context :eval)
  895.               (consp form)
  896.               (symbolp (car form)))
  897.          (let ((name (car form)))
  898.            (when (and (not (memq name functions))
  899.                   (memq name possible-functions))
  900.              (pushnew name functions)
  901.              (walk (cddr (assq name *local-cache-functions*))))))
  902.            form)
  903.          (walk (body)
  904.            (walk-form `(progn . ,body) env #'walk-function)))
  905.       (walk body)
  906.       functions)))
  907.  
  908. )
  909.  
  910.  
  911. ;;;
  912. ;;; returns 4 values, <field> <cache> <mask> <size>
  913. ;;; It tries to re-adjust the cache every time it makes a new fill.  The
  914. ;;; intuition here is that we want uniformity in the number of probes needed to
  915. ;;; find an entry.  Furthermore, adjusting has the nice property of throwing out
  916. ;;; any entries that are invalid.
  917. ;;;
  918. (defun fill-cache-internal (field cache nkeys valuep limit-fn wrappers value)
  919.   (let ((*nkeys* nkeys)
  920.     (*valuep* valuep)
  921.     (*limit-fn* limit-fn))
  922.     (with-local-cache-functions (cache)
  923.       (flet ((4-values-please (f c)
  924.            (multiple-value-bind (mask size)
  925.            (compute-cache-parameters *nkeys* *valuep* c)
  926.          (values f c mask size))))
  927.     (let ((easy-fill-p (fill-cache-p nil field cache wrappers value)))
  928.       (if easy-fill-p
  929.           (4-values-please field cache)
  930.           (multiple-value-bind (adj-field adj-cache)
  931.           (adjust-cache field cache wrappers value)
  932.         (if adj-field
  933.             (4-values-please adj-field adj-cache)
  934.             (multiple-value-bind (exp-field exp-cache)
  935.             (expand-cache field cache wrappers value)
  936.               (4-values-please exp-field exp-cache))))))))))
  937.  
  938. ;;;
  939. ;;; returns T or NIL
  940. ;;;
  941. (defun fill-cache-p (forcep field cache wrappers value)
  942.   (with-local-cache-functions (cache)
  943.     (let* ((primary (location-line (compute-primary-cache-location field (mask) wrappers))))
  944.       (multiple-value-bind (free emptyp)
  945.       (find-free-cache-line primary field cache wrappers)
  946.     (when (or forcep emptyp) (fill-line free wrappers value) t)))))
  947.  
  948. (defun fill-cache-from-cache-p (forcep field cache from-cache from-line)
  949.   (with-local-cache-functions (from-cache)
  950.     (let ((primary (line-primary field from-line)))
  951.       (multiple-value-bind (free emptyp)
  952.       (find-free-cache-line primary field cache)
  953.     (when (or forcep emptyp)
  954.       (transfer-line from-cache from-line cache free)
  955.       t)))))
  956.  
  957. (defun entry-in-cache-p (field cache wrappers value)
  958.   (declare (ignore field value))
  959.   (with-local-cache-functions (cache)
  960.     (dotimes (i (nlines))
  961.       (unless (line-reserved-p i)
  962.     (when (equal (line-wrappers i) wrappers) (return t))))))
  963.  
  964. ;;;
  965. ;;; Returns NIL or (values <field> <cache>)
  966. ;;; 
  967. ;;; This is only called when it isn't possible to put the entry in the cache
  968. ;;; the easy way.  That is, this function assumes that FILL-CACHE-P has been
  969. ;;; called as returned NIL.
  970. ;;;
  971. ;;; If this returns NIL, it means that it wasn't possible to find a wrapper
  972. ;;; field for which all of the entries could be put in the cache (within the
  973. ;;; limit).  
  974. ;;;
  975. (defun adjust-cache (field cache wrappers value)
  976.   (with-local-cache-functions (cache)
  977.     (let ((ncache (get-cache (size))))
  978.       (do ((nfield field (next-wrapper-field nfield)))
  979.       ((null nfield) (free-cache ncache) nil)
  980.     (labels ((try-one-fill-from-line (line)
  981.            (fill-cache-from-cache-p nil nfield ncache cache line))
  982.          (try-one-fill (wrappers value)
  983.            (fill-cache-p nil nfield ncache wrappers value)))
  984.       (if (and (dotimes (i (nlines) t)
  985.              (when (and (null (line-reserved-p i))
  986.                 (line-valid-p i wrappers))
  987.                (unless (try-one-fill-from-line i) (return nil))))
  988.            (try-one-fill wrappers value))
  989.           (return (values nfield ncache))
  990.           (flush-cache-internal ncache)))))))
  991.  
  992.                
  993. ;;;
  994. ;;; returns: (values <field> <cache>)
  995. ;;;
  996. (defun expand-cache (field cache wrappers value)
  997.   (declare (values field cache) (ignore field))
  998.   (with-local-cache-functions (cache)
  999.     (multiple-value-bind (ignore size)
  1000.     (compute-cache-parameters (nkeys) (valuep) (* (nlines) 2))
  1001.       (let* ((ncache (get-cache size))
  1002.          (nfield (wrapper-field 'number)))
  1003.     (labels ((do-one-fill-from-line (line)
  1004.            (unless (fill-cache-from-cache-p nil nfield ncache cache line)
  1005.              (do-one-fill (line-wrappers line) (line-value line))))
  1006.          (do-one-fill (wrappers value)
  1007.            (multiple-value-bind (adj-field adj-cache)
  1008.                (adjust-cache nfield ncache wrappers value)
  1009.              (if adj-field
  1010.              (setq nfield adj-field ncache adj-cache)
  1011.              (fill-cache-p t nfield ncache wrappers value))))
  1012.          (try-one-fill (wrappers value)
  1013.            (fill-cache-p nil nfield ncache wrappers value)))
  1014.       (dotimes (i (nlines))
  1015.         (when (and (null (line-reserved-p i))
  1016.                (line-valid-p i wrappers))
  1017.           (do-one-fill-from-line i)))
  1018.       (unless (try-one-fill wrappers value)
  1019.         (do-one-fill wrappers value))
  1020.       (values nfield ncache))))))
  1021.  
  1022.  
  1023. ;;;
  1024. ;;; This is the heart of the cache filling mechanism.  It implements the decisions
  1025. ;;; about where entries are placed.
  1026. ;;; 
  1027. ;;; Find a line in the cache at which a new entry can be inserted.
  1028. ;;;
  1029. ;;;   <line>
  1030. ;;;   <empty?>           is <line> in fact empty?
  1031. ;;;
  1032. (defun find-free-cache-line (primary field cache &optional wrappers)
  1033.   (declare (values line empty?))
  1034.   (with-local-cache-functions (cache)
  1035.     (let ((limit (funcall (limit-fn) (nlines)))
  1036.       (wrappedp nil))
  1037.       (when (line-reserved-p primary) (setq primary (next-line primary)))
  1038.       (labels (;;
  1039.            ;; Try to find a free line starting at <start>.  <primary>
  1040.            ;; is the primary line of the entry we are finding a free
  1041.            ;; line for, it is used to compute the seperations.
  1042.            ;;
  1043.            (find-free (p s)
  1044.          (do* ((line s (next-line line))
  1045.                (nsep (line-separation p s) (1+ nsep)))
  1046.               (())
  1047.            (if (null (line-valid-p line wrappers)) ;If this line is empty or
  1048.                (return (values line t))               ;invalid, just use it.
  1049.  
  1050.                (let ((osep (line-separation (line-primary field line) line)))
  1051.              (if (and wrappedp (>= line primary))
  1052.                  ;;
  1053.                  ;; have gone all the way around the cache, time to quit
  1054.                  ;; 
  1055.                  (return (values line nil))
  1056.                  
  1057.                  (when (cond ((or (= nsep limit)) t)
  1058.                      ((= nsep osep) (zerop (random 2)))
  1059.                      ((> nsep osep) t)
  1060.                      (t nil))
  1061.                    ;;
  1062.                    ;; Try to displace what is in this line so that we
  1063.                    ;; can use the line.
  1064.                    ;;
  1065.                    (return (values line (displace line)))))))
  1066.            
  1067.            (if (= line (1- (nlines))) (setq wrappedp t))))
  1068.            ;;
  1069.            ;; Given a line, attempt to free up that line by moving its
  1070.            ;; contents elsewhere. Returns nil when it wasn't possible to
  1071.            ;; move the contents of the line without dumping something on
  1072.            ;; the floor.  
  1073.            ;; 
  1074.            (displace (line)
  1075.          (if (= line (1- (nlines))) (setq wrappedp t))
  1076.          (multiple-value-bind (dline dempty?)
  1077.              (find-free (line-primary field line) (next-line line))
  1078.            (when dempty? (copy-line line dline) t))))
  1079.     
  1080.     (find-free primary primary)))))
  1081.